﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WCS.Utils;
using WCS.Helpers;
using WCS.Entity;
using WcsTask;
using WcsTask.DevCommTaks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using SqlSugar;
using Sunny.UI.Win32;

namespace WCS.Controls.系统查询
{
    public partial class AreaLocationChange : UIPage
    {
        public static AreaLocationChange _ChangeMaterialType;
        public AreaLocationChange()
        {
            InitializeComponent();
            _ChangeMaterialType = this;
        }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper SqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton26_Click(object sender, EventArgs e)
        {
            if (stationNo.Text != "" && stationNo.Text.Length == 4)
            {
                DialogResult res = MessageBox.Show($"确定修改{stationNo.Text}的物料为{MaterialTypeList.Text}吗？", "修改提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    Change(stationNo.Text, MaterialTypeList.Text, isOpen.Text);
                }
            }
            else
            {
                MessageBox.Show("站台为空或站台不存在！", "Tip");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddTask_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> PLCType = new Dictionary<string, string>();
            foreach (var item in SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>().ToList().FindAll(f => !f.MaterialType.Contains("物料")))
            {
                PLCType.Add(item.MaterialType, item.MaterialType);
            }

            MaterialTypeList.DataSource = new BindingSource(PLCType, null);
            MaterialTypeList.DisplayMember = "key";
            MaterialTypeList.ValueMember = "value";

            Re();
        }

        public void Re()
        {
            this.uiDGAreaLocation.DataSource = SqlSugarDapper.DB.Queryable<AreaLocation>().Where(w => w.BinCode == 1).OrderBy(o => o.StationNo).ToList();
        }

        public void Change(string stationNo, string materialType, string enable)
        {
            //删除完成读取是否还有托盘
            SqlSugarDapper.DB.Updateable<AreaLocation>()
                .SetColumns(s => s.MaterialType == materialType)
                .SetColumns(s => s.Enable == enable)
                .Where(w => w.StationNo == stationNo)
                .ExecuteCommand();
            Re();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton24_Click(object sender, EventArgs e)
        {
            Re();
        }

       
    }
}
